package org.yoki.tool.core.config;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by SunnyWen on 2017/6/24.
 */
public class BuilderConfig {

    private String title = "测试系统";
    private String savePath = "C:\\Users\\SunnyWen\\Desktop\\code";
    private String projName = "yoki" ;
    private String projVersion = "1.0-SNAPSHOT" ;
    private String mavenName = "" ;
    private String groupId = "org.yoki.tool";
    private String moduleName = "core";

    private String templatePath = "classpath:tamplates";

    private DatabaseConfig databaseConfig ;

    private Configuration configuration;

    private Map<String , Template> templateMap = new HashMap<>();

    public BuilderConfig(DatabaseConfig databaseConfig){
        this.databaseConfig = databaseConfig ;
    }

    public BuilderConfig(String url , String username , String password){
        this.databaseConfig = new DatabaseConfig(url , username , password) ;
    }

    public DatabaseConfig getDatabaseConfig() {
        return databaseConfig;
    }

    public void setDatabaseConfig(DatabaseConfig databaseConfig) {
        this.databaseConfig = databaseConfig;
    }

    public Configuration getTemplateCfg() {
        if (null == configuration) {
            configuration = new Configuration(Configuration.VERSION_2_3_21);
            String path;
            if (templatePath.contains("classpath:")) {
                String temp = templatePath.replace("classpath:", "");
                path = getClass().getResource("/").getPath() + temp;
            } else {
                path = templatePath;
            }
            File f = null;
            try {
                f = new File(path);
            } catch (Exception e) {
                System.out.println("读取文件出错!!请确认" + templatePath + "路径下文件或文件夹是否存在!!!");
                e.printStackTrace();
            }

            try {
                configuration.setDirectoryForTemplateLoading(f);
            } catch (Exception e) {
                System.out.println("加载模板文件出错!!");
                e.printStackTrace();
            }
            File[] childFiles = f.listFiles();
            for (File cf : childFiles) {
                String fileName = cf.getName() ;
                if (fileName.endsWith(".ftl")){
                    try {
                        Template template= configuration.getTemplate(fileName);
                        templateMap.put(template.getName() , template);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return configuration;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public String getProjName() {
        return projName;
    }

    public void setProjName(String projName) {
        this.projName = projName;
    }

    public String getProjVersion() {
        return projVersion;
    }

    public void setProjVersion(String projVersion) {
        this.projVersion = projVersion;
    }

    public String getMavenName() {
        return mavenName;
    }

    public void setMavenName(String mavenName) {
        this.mavenName = mavenName;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getTemplatePath() {
        return templatePath;
    }

    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }

    public Map<String , Template> getTemplateMap() {
        if(null == configuration)
            getTemplateCfg() ;
        return templateMap;
    }

    public void setTemplateMap(Map<String , Template> templateMap) {
        this.templateMap = templateMap;
    }
}
